package rs.etf.es.algorithm;

import java.io.Serializable;
import java.util.HashMap;

public class ConditionalProbabilityTable implements Serializable {

    private HashMap<String, HashMap<String, HashMap<String, Double>>> probabilitiesMap;

    public ConditionalProbabilityTable() {
	super();

	probabilitiesMap = new HashMap<String, HashMap<String, HashMap<String, Double>>>();
    }

    public Double getProbability(String parentName, String myVariable, String parentVariable) {

	if (parentName == null || myVariable == null || parentVariable == null)
	    return null;

	Double result = null;
	HashMap<String, HashMap<String, Double>> parentsMap = probabilitiesMap.get(parentName);
	if (parentsMap == null) {
	    return null;
	} else {
	    HashMap<String, Double> valuesMap = parentsMap.get(myVariable);
	    if (valuesMap == null) {
		return null;
	    } else {
		result = valuesMap.get(parentVariable);
	    }
	}

	return result;
    }

    public void setProbability(String parentName, String myVariable, String parentVariable, Double probability) throws Error {
	if (parentName == null || myVariable == null || parentVariable == null || probability == null)
	    return;

	HashMap<String, HashMap<String, Double>> map = probabilitiesMap.get(parentName);

	if (map == null) {
	    throw new Error("Invalid parent name");
	} else {
	    HashMap<String, Double> newMap = map.get(myVariable);
	    if (newMap == null) {
		throw new Error("Invalid myVariable name");
	    } else {
		newMap.put(parentVariable, probability);
	    }
	}
    }

    public HashMap<String, HashMap<String, HashMap<String, Double>>> getProbabilitiesMap() {
	return probabilitiesMap;
    }

    public void setProbabilitiesMap(HashMap<String, HashMap<String, HashMap<String, Double>>> probabilitiesMap) {
	this.probabilitiesMap = probabilitiesMap;
    }

}
